<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$fenlei=pdo_getall('jy_help_group',array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_weishop'),array('id','title'),'id');
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$fenleiid=intval($_RQ['fenlei']);
	if($fenleiid){
		$params['fenlei']=$fenleiid;
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$tip='如果场景前端（包含DIY页面）支持展示问题的时候，前端才会问题出来！！';
	$onepluginname=model('jy_weishop/goods')->is_onegoods($_SESSION['current_plugin'],$_RQ['pluginname']);
	if($onepluginname){//开启独立商品
			$params['plugin_name']=$onepluginname;
			$return['params']['plugin_name']=$onepluginname;
			$tip .='</br><red>【注意】：本场景已开启独立商品展示，只显示本场景下的问答！</red>';
	}else{
			$params['plugin_name OR']='plugin_name=\'jy_weishop\' or plugin_name=\''.$_SESSION['current_plugin'].'\'';
			$tip .='</br><red>【注意】：本场景已开启独立商品展示，只显示本场景下的问答！</red>';
	}
	$fields=array('id','title','group_id','image','createtime','readnum','likenum','status');
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_help_list',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['content'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['content'],0,36);
			$item['createtime']=date('y/m/d H:i:s',$item['createtime']);
			$item['url']=custom_url('jy_help/detail//id='.$item['id'],true);
		}
	}
	$return['list']=$list?$list:array();
	$return['fenlei']=$fenlei?$fenlei:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'问题列表'),
			array('op'=>'edit','title'=>'添加问题'),
		),
		'list'=>array('data'=>'list','tip'=>$tip,'edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'问题标题','type'=>'string'),
			array('type'=>'select','title'=>'问题分类','name'=>'group_id','options'=>'fenlei'),
			array('name'=>'readnum','title'=>'阅读量','type'=>'string'),
			array('name'=>'likenum','title'=>'点赞量','type'=>'string'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
		),'action'=>array(
			array('op'=>'edit','title'=>'编辑','type'=>'href'),
		)),
		'search_one'=>array('name'=>'title','help'=>'','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	pdo_update('jy_help_list',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_help_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_help_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='edit'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_weishop');
	$return['fenlei']=pdo_getall('jy_help_group',$params,array('id','title'));
	$id = intval($_RQ['id']);
	if($id){
		$return['item']=pdo_get('jy_help_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$return['item']=array('advs'=>array());
	}
	$htmls=array(
			'type'=>'edit',
			'post'=>'post',
			'desc'=>'问题信息',
			'tabs'=>array(
				array('op'=>'list','title'=>'问题列表'),
				array('op'=>'edit','title'=>'编辑列表'),
			),
			'edit'=>array(
				'0'=>array('type'=>'string','title'=>'问题标题','name'=>'title', 'help'=>'' ,'must'=>true),
				'1'=>array('type'=>'string','title'=>'问题排序','name'=>'paixu', 'help'=>'填写100以内正整数，数值越小越靠前' ),
				'2'=>array('type'=>'switch','title'=>'是否显示','name'=>'status','help'=>'' ),
				'3'=>array('type'=>'select','title'=>'问题分类','name'=>'group_id','options'=>'fenlei'),
				'4'=>array('type'=>'richtext','title'=>'问题内容','name'=>'content','help'=>'','must'=>true),
			)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$fields=array('title','group_id','readnum','likenum','status','desc','image','content');
	$data=array_fields($fields,$postdata);
	if(empty($data['title'])){
		exi('请填写问题标题！','error');
	}
	if(empty($data['group_id'])){
		exi('请填写问题分类！','error');
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_help_list',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['plugin_name']='jy_weishop';
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_help_list',$data);
		$id=pdo_insertid();
	}
	exi('编辑问题成功！','','list');
}

